package com.example.auth.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.auth.model.MenuModel;
import com.example.auth.model.RoleUserModel;
import com.example.auth.model.UserModel;
import com.example.auth.service.MenuModelService;
import com.example.auth.service.RoleMenuModelService;
import com.example.auth.service.RoleUserModelService;
import com.example.auth.service.UserModelService;
import com.example.utils.Results;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname MenuController
 * @Description TODO
 * @Date 2022/7/29 10:40
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController(value = "/menu",desc = "菜单服务模块")
public class MenuController {

    @Autowired
    UserModelService userModelService;

    @Autowired
    MenuModelService menuModelService;

    @Autowired
    RoleUserModelService roleUserModelService;

    @Autowired
    RoleMenuModelService roleMenuModelService;

    @Auth
    @GetMapping(value = "/getmenuList")
    public JSONObject getMenuListInfo(){
       List<MenuModel> menuModelList= menuModelService.getMenuModels();
       return Results.success(menuModelList);
    }

    @Auth
    @GetMapping(value = "/getchildList")
    public JSONObject getMenuChildListController(){
        List<MenuModel> menuModelList = menuModelService.getMenuModelsByUid("1");
        return Results.success(menuModelList);
    }

    @GetMapping(value = "/getmenuChildInfo")
    public JSONObject getMenuChildInfoController(){
        JSONObject homeInfo=new JSONObject();
        homeInfo.put("title","首页");
        homeInfo.put("href","page/welcome-1.html?t=1");
        JSONObject logoInfo=new JSONObject();
        logoInfo.put("title","start-prd");
        logoInfo.put("image","");
        logoInfo.put("href","");
        List<MenuModel> menuModelList = menuModelService.getMenuModelsByUid("1");
        JSONObject result=new JSONObject();
        result.put("homeInfo",homeInfo);
        result.put("logoInfo",logoInfo);
        result.put("menuInfo",menuModelList);
        return Results.success(result);
    }

    @GetMapping("/getmenuListbyUser")
    public JSONObject getMenuListController(){
        //获取当前登录的用户
        UserModel userModel=userModelService.getUserModel();
        //查询用户的角色列表
        List<Integer> roleIds=roleUserModelService.getRoleModelRoleIdsByUid(userModel.getId());
        //菜单列表
        List<Integer> menuIds=roleMenuModelService.getRoleMenuListRoleIds(roleIds);

        JSONObject homeInfo=new JSONObject();
        homeInfo.put("title","首页");
        homeInfo.put("href","page/welcome-1.html?t=1");
        JSONObject logoInfo=new JSONObject();
        logoInfo.put("title","start-prd");
        logoInfo.put("image","");
        logoInfo.put("href","");
        JSONObject result=new JSONObject();
        result.put("homeInfo",homeInfo);
        result.put("logoInfo",logoInfo);
        //菜单去重
        if(menuIds.size() == 0){
            result.put("menuInfo",Collections.emptyList());
            return Results.success(result);
        }
        //对菜单去重
        menuIds=menuIds.stream().distinct().collect(Collectors.toList());
        List<MenuModel> menuModelListTree=menuModelService.getMenuModelsByIds(menuIds);
        result.put("menuInfo",menuModelListTree);
        return Results.success(result);
    }

    @GetMapping(value = "/getmenutablechildListall",desc = "获取所有的菜单")
    public JSONObject getMenuTableChildListAllController(){
        List<MenuModel> menuModelList=menuModelService.getMenuModels();
        return Results.success(menuModelList);
    }

    @PostMapping(value = "/save",desc = "新增菜单")
    public JSONObject saveMenuModel(@RequestBody MenuModel menuModel){
        if(menuModel.getId()!=null){
            menuModelService.updateMenuModelById(menuModel);
            return Results.success("修改成功");
        }else {
            menuModel.setCreatedate(new Date());
            menuModel.setIdDel(0);
            menuModel.setIsmenu(0);
            System.out.println(JSONObject.toJSONString(menuModel));
            menuModelService.saveMenuModel(menuModel);
            return Results.success("新增成功");
        }
    }

    @GetMapping(value = "/getone",desc = "获取一个")
    public JSONObject getMenuModelOneController(@Param("id")String id){
       MenuModel menuModel=menuModelService.getMenuModelById(id);
       return Results.success(menuModel);
    }



}
